package com.fernandomdo.ServiceAdministrator;

import java.util.Calendar;

import com.fernandomdo.ServiceAdministrator.databaseInterface.DatabaseInterface;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class ConfirmDialog extends DialogFragment {
	
	public static final int ACTION_CHECKIN = 0;
	public static final int ACTION_CHECKOUT = 1;
	public static final int ACTION_EDIT = 2;
	public static final int ACTION_DELETE = 3;
	
	public final static String TERRITORY_ID = "territory_id";
	public final static String DIALOG_TYPE = "type";
	public final static String DIALOG_TITLE = "title";
	public final static String CHECKIN = "confirm_checkin";
	public final static String DELETE = "confirm_delete";
	
	private static boolean initFromFragment = false;
	
	private static DatabaseInterface databaseInterface;
	
    public static ConfirmDialog newInstance(DatabaseInterface dbInterface, int confirmType, int title, long territoryId, boolean initFromFrag) {
        ConfirmDialog frag = new ConfirmDialog();
        Bundle args = new Bundle();
        args.putInt(ConfirmDialog.DIALOG_TITLE, title);
        args.putLong(ConfirmDialog.TERRITORY_ID, territoryId);
        args.putInt(ConfirmDialog.DIALOG_TYPE, confirmType);
        frag.setArguments(args);
        initFromFragment = initFromFrag;
        databaseInterface = dbInterface;
        
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title 	 	= getArguments().getInt(ConfirmDialog.DIALOG_TITLE);
        int positiveAction;
        final long id 	= getArguments().getLong(ConfirmDialog.TERRITORY_ID);
        final int type 	= getArguments().getInt(ConfirmDialog.DIALOG_TYPE);
        
        switch(type) {
        case ACTION_CHECKIN:
        	positiveAction = R.string.action_check_in;
        	break;
        case ACTION_DELETE:
        	positiveAction = R.string.action_delete_territory;
        	break;
        default:
        	positiveAction = R.string.action_ok;
        	break;
        }
        
        return new AlertDialog.Builder(getActivity())
                //.setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(positiveAction,
                    new DialogInterface.OnClickListener() {
                        @Override
						public void onClick(DialogInterface dialog, int whichButton) {
                        	switch(type) {
                        	case ACTION_CHECKIN:
                        		ListTerritoryFragment.confirmCheckin(id);
                        		break;
                        	case ACTION_DELETE:
	                            ListTerritoryFragment.confirmDelete(id);
                        	}
                        }
                    }
                )
                .setNegativeButton(R.string.action_cancel,
                    new DialogInterface.OnClickListener() {
                        @Override
						public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }
                )
                .create();
    }	
    
    public static void confirmDelete(long territoryId) {
		databaseInterface.deleteTerritory(territoryId);	
		if(initFromFragment)
			ListTerritoryFragment.updateList();
	}
	
	public static void confirmCheckin(long territoryId) {
		databaseInterface.checkInTerritory(territoryId, Calendar.getInstance().getTime());	
		if(initFromFragment)
			ListTerritoryFragment.updateList();
	}
}